/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			ETVpCmd_EditSubI.h
  \date			Sep 2011
  \author		TNick

  \brief		Contains the definition for ETVpCmd_EditSubI class


  Copyright (c) 2011, Nicu Tofan
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
 * Neither the name of the copyright holder nor the
   names of its contributors may be used to endorse or promote products
   derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL NICU TOFAN BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
#ifndef __ETVPCMD_EDITSUBI_INC__
#define __ETVPCMD_EDITSUBI_INC__
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

class		ETI_CplxTxt;
class		ETSI_SText;
class		ETVpCmd_EditSubI;


#include	<QLineEdit>
#include    <QTransform>

#include	<sheet/ETVpCommand.h>

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */


/// line editor for simple text input
class	LnEditSI	: public QLineEdit	{
	Q_OBJECT

private:

	/// associated class instance that gets notified about events
	ETVpCmd_EditSubI * requester;


public:

	/// constructor; prepares and shows the editor
	LnEditSI(
			ETVpCmd_EditSubI *	ed_req,	/**< the instance of this class
	that created the widget */
			QString				s_tx,	/**< initial text to show */
			QRect				geom,	/**< where to place inside parent */
			ETSheetView *		parent	/**< the widget that will host this
	line editor */
			);

protected:

	/// filter Return and Escape keys
	void 	keyPressEvent ( QKeyEvent * event );

	/// based on options, this may tigger a close
	void 	focusOutEvent ( QFocusEvent * e );

};	/* class	LnEditSI */

/*  CLASS    =============================================================== */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */


/// viewport command that edits an sub-item
/**
*	The command may be called for a given sub-item or it may be started for
*	the current item. If there is no current item, the command will fail
*	with an error message.
*
*	Once the sub-item is determined the function saves the current view matrix
*	that will be restored uppon command completion. Then, a proper view
*	is determined so that the rotation is 0 and the item is properly visible.
*	The view is also translated so thatat least top left corner is visible.
*
*	Once the location is settled a class inheriting
*	<a href="http://doc.qt.nokia.com/latest/qlineedit.html">QLineEdit</a>
*	is parented in the viewport and filled with the item's text. The user is
*	able to change the text.
*
*	Hitting Escape key will cancel the change, as it only tiggers the cancel()
*	signal. Hitting Return key or clicking outside the control tiggers the
*	accepted() signal. Note that loosing focus does not closes the editor.
*
*	@todo close editor on focus lost based on settings
*
*	The accepted() function will change the text of the sub-item and the
*	ETI_CplxTxt item will be updated.
*/
class ETVpCmd_EditSubI : public ETVpCommand		{

	//
	//
	//
	//
	/*  DEFINITIONS    ----------------------------------------------------- */


	/*  DEFINITIONS    ===================================================== */
	//
	//
	//
	//
	/*  DATA    ------------------------------------------------------------ */

private:


	/// associated editor
	LnEditSI *		p_editor;

	/// the item that we're editing
	ETI_CplxTxt *	it_in_ed;

	/// the sub-item that is being edited
	ETSI_SText *	sit_in_ed;

	/// the viewport that we're editing
	ETSheetView *	viwp;

	/// saved transformation for the view
	QTransform		transf;



	/*  DATA    ============================================================ */
	//
	//
	//
	//
	/*  FUNCTIONS    ------------------------------------------------------- */

public:

	/// constructor;
	ETVpCmd_EditSubI		( void );

	/// destructor;
	virtual				~ETVpCmd_EditSubI		( void );


	/// the class is informed that the command is starting
	/**
  *	Commands are started by creating an instance of a class inherited from
  *	ETVpCommand and passing it to startCommand(). While current implementation
  *	will always call ETVpCommand::start(), this may change in the future. As a
  *	result the command (class) shall only initialise itself when it recieves
  *	the ETVpCommand::start(), not in constructor.
  *
  *	@return		true to continue loading or false to cancel
  */
	bool				start		(
			ETSheetView *		trg_vp		/**< target viewport */
			);


	/// the class is informed that the command is being canceled
	/**
  *	The call to this function may or may not be preceded by a call to
  *	accepted(), depending on the user actions. It is usually called
  *	just before destroying the instance. However, the implementations
  *	shall free allocated resources here and not in destructor.
  */
	void				cancel		(
			ETSheetView *		trg_vp		/**< target viewport */
			);


	/// the class is informed that the command is being accepted
	/**
 *	The command may or may not implement special functionality for this
 *	event. It is similar to an Ok button pushed in a dialog. The class
 *	is guaranteed to recieve a cancel() call just after the call to this
 *	function without any other calls to this class inbetween.
 */
	void			accepted		(
			ETSheetView *		trg_vp		/**< target viewport */
			);


	/// called before start() to set the item that's being edited
	void		setSubItemToEdit	(
			ETI_CplxTxt *	new_it_in_ed,
			ETSI_SText *	new_sit_in_ed
			);


protected:

	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
	/** @name Events forwarded by the viewport    */
	//@{
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

	bool		mousePress	(
			ETSheetView *	vp,
			QMouseEvent * event);


	//@}
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */





	/*  FUNCTIONS    ======================================================= */
	//
	//
	//
	//

};	/*	class ETVpCmd_EditSubI	*/

/*  CLASS    =============================================================== */
//
//
//
//
#endif // __ETVPCMD_EDITSUBI_INC__
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
